import Vue from "vue";
import VueRouter from "vue-router";
import App from "./App.vue";
import "./public-path";
import routes from "./router";
import store from "./store";
import "./plugins/element.js"; // 引用 element-ui
import "@/assets/css/demo.min.css";
import routeMatch from "@/auth/route-match"; // 导入路由匹配文件路径函数

Vue.config.productionTip = false;

let router = null;
let instance = null;
const __qiankun__ = window.__POWERED_BY_QIANKUN__; // 判断是否是 qiankun 环境
console.log("是否是 qiankun 环境", __qiankun__);
export async function bootstrap(props) {
  console.log('props',props)
  // 这里是获取到子应用的 propsData
  // 注册主应用下发的组件
  // Vue.use(components);
  // // 把工具函数挂载在vue $mainUtils对象
  // Vue.prototype.$mainUtils = utils;
  // // 把mainEmit函数一一挂载
  // Object.keys(emitFnc).forEach(i => {
  //     Vue.prototype[i] = emitFnc[i]
  // });
  // // 在子应用注册呼机
  // pager.subscribe(v => {
  //     console.log(`监听到子应用${v.from}发来消息：`, v)
  //         // store.dispatch('app/setToken', v.token)
  // })
  // Vue.prototype.$pager = pager;
  // // 在子应用注册官方通信
  // /* actions.onGlobalStateChange((state, prev) => console.log(`子应用subapp-ui监听到来自${state.from}发来消息：`, state, prev)); */
  // Vue.prototype.$actions = actions;
}

export async function mount({ data = {}, ROUTES, routerBase } = {}) {
  console.log(
    "mount data",
    data,
    ROUTES,
    routerBase,
    "routerMatch",
    routeMatch(ROUTES, routerBase)
  );
  router = new VueRouter({
    base: __qiankun__ ? routerBase : "/", // 需配置一个 前缀路由
    mode: "history",
    routes: __qiankun__ ? routeMatch(ROUTES, routerBase) : routes,
  });
  instance = new Vue({
    router,
    store,
    render: (h) =>
      h(App, {
        props: { ...data },
      }),
  }).$mount("#subApp");
}

export async function unmount() {
  instance.$destroy();
  instance = null;
  router = null;
}

// 单独开发环境
__qiankun__ || mount();
